﻿var VendorGrid = VendorGrid || {};
VendorGrid = function () {
    var gridInEditMode = false;
    var scratchDropDownChanged = false;
    var VendorDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "/Vendor/GetVendorCompanies",
                type: "get",
                cache: false,
                dataType: "json"
            },


            parameterMap: function (options) {
                return kendo.stringify(options); // kendo.stringify serializes to JSON string
            }

        },

        pageSize: 5,
        schema: {
            model: {
                id: "companyId",
                fields: {
                    
                    companyId:
                        {
                            type: "int",
                            editable: false, nullable: true,
                        },
                    description: {

                        type: "string",
                        validation: { required: true }
                    },

                    error: function (a) {
                        $('#VendorGrid').data("kendoGrid").cancelChanges();
                    },

                }
            }
        }

    });

    
    return {
        VendorDataSource: VendorDataSource,
        gridInEditMode: gridInEditMode,
        scratchDropDownChanged: scratchDropDownChanged
    }
}();




$(function () {

    $(window).on("beforeunload", function () {

        if (VendorGrid.gridInEditMode) {

            return "Are you sure you want to discard all your changes."

        }

    });
   


  
    $("#VendorGrid").kendoGrid({
        dataSource: VendorGrid.VendorDataSource,
        scrollable: true,
        height: "250px",
      //  toolbar: ["create"], //, "save", "cancel"],
        //toolbar:["create"],
        pageable: true,
        height: 300,
      
        columns: [
      { title: "Products", filterable: false, width: "8em", template: "<a onclick=\"showVendorProducts('#=id #',this);\"style=\"cursor: pointer;\"> View</a>" },
      { field: "companyname", title: "Vendor Company", filterable: true, width: "8em" },    
     
      {
          title: "&nbsp;",
          width: "25em",
          //command: ["edit", "destroy"]
          command: [

              "edit",
              {
                  name: "Delete",


                  click: function (e) {
                     
                      var tr = $(e.target).closest("tr"); //get the row for deletion
                      var dataItem = this.dataItem(tr); //get the row data so it can be referred later
                      //see what is coming in dataItem
                      //if (dataItem > 0) {
                      //    $("#delValidation").addClass('in')
                      //}
                      //else {

                          deleteTopicConfirmation(e, this);
                      ////}
                      return false;
                  }
              }
          ]
      }
        ],
        filterable: {
            extra: false,
            operators: {
                string: {
                    eq: "Is equal to",
                    neq: "Is not equal to",
                      contains:"Contains"
                }
            }
        },
      

        editable: "popup",
        sortable: {
            mode: "single",
            allowUnsort: false
        },

      
    
        selectable: true,
       
     
        cancel: function () {
            VendorGrid.gridInEditMode = false;
        }
      
    });

    VendorGrid.VendorDataSource.bind("requestEnd", showSuccessMessage);


    function showSuccessMessage(e) {
        if (e.type === "update" || e.type === "create") {
            VendorGrid.gridInEditMode = false;
            $("#saveConfirm").addClass('in');
            e.sender.read();
        }
    }


    $(".close").click(function () {
        $(this).parent().removeClass('in');
    });


    function deleteTopicConfirmation(e, widget) {
        var grid = $('#VendorGrid').data("kendoGrid");
        deleteConfirmation(e, widget, grid);
    }

    function deleteConfirmation(e, widget, grid) {

        var currentRow = $(e.target).closest("tr"); //get the row for deletion

        var currentRowData = widget.dataItem(currentRow); //get the row data so it can be  referred later

        var kendoWindow = $("<div />").kendoWindow({

            title: "Confirmation",
            resizable: true,
            modal: true,
            width: "400px"
        });

        kendoWindow.data("kendoWindow")

            .content($("#delete-confirmation").html())

            .center().open();

        kendoWindow

            .find(".delete-confirm,.delete-cancel")
            .click(function () {
                if ($(this).hasClass("delete-confirm")) {

                    grid.dataSource.remove(currentRowData);

                    grid.dataSource.sync();

                }

                $(this).closest("[data-role=window]").kendoWindow("close");

            });

    }

});